package array.medium;

/**
 * 删除有序数组的重复项
 *
 * 示例 1：
 * 输入：nums = [1,1,1,2,2,3]
 * 输出：5, nums = [1,1,2,2,3]
 *
 * 示例 2：
 * 输入：nums = [0,0,1,1,1,1,2,3,3]
 * 输出：7, nums = [0,0,1,1,2,3,3]
 *
 * 链接：https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii
 */
public class RemoveDuplicatesFromSortArrayII_80 {

    public int removeDuplicates(int[] nums) {
        int i = 0, j = 0;
        while(j < nums.length) {
            //[1, 2, 2, 3]
            if ((i == 0 || i == 1) || (nums[j] != nums[i - 2])) {
                nums[i++] = nums[j++];
            } else {
                j++;
            }
        }

        return i;
    }

    public static void main(String[] args) {
        RemoveDuplicatesFromSortArrayII_80 rdfs = new RemoveDuplicatesFromSortArrayII_80();
        rdfs.removeDuplicates(new int[]{0,0,1,1,1,1,2,3,3});
    }
}
